package com.linewell.dao;

import com.linewell.entity.Reply;
import com.linewell.entity.ReplyVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Title: ssm2
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/2/21
 */
public interface CommentMapper {

    @Insert("insert into comment values(${Id},${articleId},${articleUserId})")
    void insertComment(@Param("Id")long Id,@Param("articleId")long articleId, @Param("articleUserId")long articleUserId );

    @ResultMap("com.linewell.dao.CommentMapper.Reply")
    @Select("select r.*,u.*,u1.user_id u1UserId,u1.nick_name u1NickName,u1.picture u1Picture  " +
            "from comment c,reply r,userinfo u,userinfo u1 " +
            "where c.article_id = ${articleId} " +
            "and c.comment_id = r.comment_id " +
            "and r.reply_type=0 " +
            "and c.article_user_id =r.reply_to_id " +
            "and reply_to_id = u.user_id  " +
            "and r.reply_from_id = u1.user_id  " +
            "order by newid() Offset (${page}-1)*5 Row Fetch Next 5 Rows Only")
    List<Reply> getReply(@Param("articleId") long articleId,@Param("page") int page);


    @Insert("insert into reply values(#{replyId},#{commentId},#{replyDate},#{replyType},#{replyText},#{replyFromId.userId},#{replyToId.userId},#{replyZan},#{replyCai},#{isCheck})")
    void insertReply(Reply reply);

    @ResultMap("com.linewell.dao.CommentMapper.Reply")
    @Select("select * FROM article a,userinfo u,comment c,reply r " +
            "where c.article_user_id = ${userId} " +
            "and c.comment_id = r.comment_id " +
            "and c.article_id = a.article_id " +
            "and c.article_user_id = u.user_id " +
            "and r.reply_type = 0" +
            "order by (r.reply_date) DESC Offset (${page}-1)*5 Row Fetch Next 5 Rows Only")
    List<Reply> getCommentByUserId(@Param("userId") long userId, @Param("page")int page);

    @Delete("DELETE from comment where comment_id = ${replyId} " +
            "DELETE from commentpoint where reply_id = ${replyId}" +
            "DELETE from reply where reply_id = ${replyId}")
    boolean deleteComment(@Param("replyId")long replyId);

    @ResultMap("com.linewell.dao.CommentMapper.Reply")
    @Select("select r.*,u.*,u1.user_id u1UserId,u1.nick_name u1NickName,u1.picture u1Picture  " +
            "from comment c,reply r,userinfo u,userinfo u1 " +
            "where c.article_id = ${articleId}  " +
            "and r.reply_type =0  " +
            "and c.comment_id = r.reply_id " +
            "and r.reply_from_id = u.user_id " +
            "order by (r.reply_zan) Desc  " +
            "Offset 0 Row Fetch Next 1 Rows Only ")
    Reply godComment(@Param("articleId")long articleId);

    @ResultMap("com.linewell.dao.CommentMapper.HotReply")
    @Select("select r.*,u.user_id,u.nick_name,u.picture,a.* "+
           "from reply r,userinfo u,article a,comment c "+
           "where r.reply_type =0  "+
           "and r.reply_from_id = u.user_id "+
           "and r.comment_id = c.comment_id "+
           "and c.article_id = a.article_id "+
           "order by (r.reply_zan) Desc " +
            "Offset ${page - 1} * 10 Row Fetch Next 10 Rows Only")
    List<Reply> getHotReply(@Param("page") int page);

    @ResultMap("com.linewell.dao.CommentMapper.Reply")
    @Select("select r.*,u.*,u1.user_id u1UserId,u1.nick_name u1NickName,u1.picture u1Picture  " +
            "from reply r,userinfo u,userinfo u1 " +
            "where r.comment_id = ${replyId} " +
            "and r.reply_type=1 " +
            "and r.reply_to_id = u.user_id  " +
            "and r.reply_from_id = u1.user_id ")
    List<Reply> getReplyFromReply(@Param("replyId")Long replyId);

    @Select("select count(*) from reply where is_check = 0")
    int getReplyCount();

    @Select("select reply_id,reply_date,reply_text,nick_name reply_from_id,is_check " +
            "from reply,userinfo " +
            "where reply.reply_from_id =  userinfo.user_id  and is_check = 0 " +
            "order by reply_date Desc Offset (${page}-1)*${limit} Row Fetch Next ${limit} Rows Only")
    List<ReplyVo> getReplyList(@Param("page")int page, @Param("limit")int limit);

    @Update("update reply set is_check = 1 where reply_id = ${replyId}")
    void updataReplyCheck(@Param("replyId")Long replyId);

    @Delete("DELETE from reply where reply_id = ${replyId}")
    boolean deleteReplyByReplyId(@Param("replyId")Long replyId);

    @Update("update reply set is_check = 1 where reply_id in (${replyIds})")
    boolean updataAllReply(@Param("replyIds")String replyIds);
}
